<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search
						borderColor="#DADADA"
						bgColor="#ffffff"
						placeholder="搜索项目名称"
						v-model="searchForm.project_name"
						:showAction="false"
						@search="searchFunc"
						@clear="searchFunc"
					></u-search>
					<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
						筛选
						<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
					</view>
					<u-button
						@click="toAdd"
						v-if="$isPer(per_arr, ['constructionLog_add'])"
						customStyle="width: auto;height: 64rpx;background: #3B75FF;border-radius: 32rpx;border-color:#F8F8FA;font-weight: 400;font-size: 26rpx;color: #ffffff;margin-left:20rpx;padding: 0 20rpx"
						:hairline="false"
						text="新增日志"
					></u-button>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging ref="paging" :fixed="false" v-model="dataList" @query="queryList" :paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items flex ac flex-wrap" :key="index" v-for="(item, index) in dataList" @click.stop="navToDetailFunc(item)">
					<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('rz_xmmc')">
						<view class="title u-line-1">{{ item.project_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('rz_xmbh')">
						<view class="title">项目编号</view>
						<view class="desc u-line-1">{{ item.project_number }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('rz_xmpp')">
						<view class="title">项目品牌</view>
						<view class="desc u-line-1">{{ item.brand_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('rz_xmlx')">
						<view class="title">项目类型</view>
						<view class="desc u-line-1">{{ item.store_type_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('rz_fxbq')">
						<view class="title">风险标签</view>
						<view class="desc u-line-1">{{ item.risk_label || '--' }}</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('rz_sgjd')">
						<view class="title">施工阶段</view>
						<view class="desc u-line-1">{{ item.construction_stage || '--' }}</view>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>项目类型</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.store_type_id.page != 1"
							@click="retractFunc('store_type_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('store_type_id', item)"
							class="search-items-list-item"
							:class="item.id == searchForm.store_type_id ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.store_type_id.list"
							:key="index"
						>
							{{ item.store_type_name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('store_type_id')"
							v-if="searchListObj.store_type_id.list.length != searchListObj.store_type_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>风险标签</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('risk_label', item)"
							:class="searchForm.risk_label == item.label ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.risk_label.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>施工阶段</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.construction_stage_id.page != 1"
							@click="retractFunc('construction_stage_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('construction_stage_id', item)"
							class="search-items-list-item"
							:class="item.id == searchForm.construction_stage_id ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.construction_stage_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('construction_stage_id')"
							v-if="searchListObj.construction_stage_id.list.length != searchListObj.construction_stage_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>是否超期</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('is_overdue', item)"
							class="search-items-list-item"
							:class="item.value == searchForm.is_overdue ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.is_overdue.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">预计完工日期</view>
					<view @click="chooseDateFunc">
						<u--input readonly placeholder="预计完工日期" border="surround" :value="searchForm.estimated_completion_date_show"></u--input>
					</view>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button
					@click="reSearchValFunc"
					text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
				<u-button
					@click="searchFunc"
					text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
			</view>
		</u-popup>
		<!-- 创建时间筛选 -->
		<u-calendar
			:show="dateShow"
			mode="range"
			@confirm="dateConfirmFunc"
			@close="dateShow = false"
			:maxDate="maxDate"
			:minDate="minDate"
			:defaultDate="defaultDate"
			:monthNum="36"
		></u-calendar>
	</view>
</template>

<script>
export default {
	name: 'constructionLog',
	props: {
		conentHeight: {
			default: 0
		},
		option: {
			type: Object,
			default: {}
		}
	},
	data() {
		const d = new Date();
		const year = d.getFullYear();
		let month = d.getMonth() + 1;
		month = month < 10 ? `0${month}` : month;
		const date = d.getDate();
		return {
			per_arr: [],
			defaultDate: [`${year}-${month}-${date}`],
			maxDate: `${year}-${month}-${date}`, //日历最大日期
			minDate: `${year - 2}-${month}-${date}`, //日历最小日期
			dateShow: false,
			searchShow: false,
			dataList: [],
			searchForm: {
				project_name: '',
				store_type_id: '',
				risk_label: '',
				construction_stage_id: '',
				is_overdue: '',
				estimated_completion_date_start: '',
				estimated_completion_date_end: '',
				estimated_completion_date_show: ''
			},
			topHeight: 0,
			loading: false,
			searchListObj: {
				store_type_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				risk_label: {
					list: [
						{
							label: '一次预警'
						},
						{
							label: '二次预警'
						},
						{
							label: '已超期'
						},
						{
							label: '停工'
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				construction_stage_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				is_overdue: {
					list: [
						{
							label: '是',
							value: 1
						},
						{
							label: '否',
							value: 2
						}
					],
					page: 1,
					num: 5,
					max: 0
				}
			}
		};
	},
	mounted() {
		this.getStoreTypeList();
		this.getConstructionStage();
		this.getHeight();

		// 获取权限
		this.$Api
			.getHtmlAuthority({
				path: '/constructionLog'
			})
			.then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
	},
	watch: {},
	methods: {
		// 重置弹窗筛选项
		reSearchValFunc() {
			this.searchForm = {
				project_name: '',
				store_type_id: '',
				risk_label: '',
				construction_stage_id: '',
				is_overdue: '',
				estimated_completion_date_start: '',
				estimated_completion_date_end: '',
				estimated_completion_date_show: ''
			};
			this.$nextTick(() => {
				this.searchFunc();
			});
		},
		// 刷新列表
		searchFunc() {
			this.$refs.paging.reload();
			this.searchShow = false;
		},
		getHeight() {
			// 创建节点查询
			let that = this;
			const systemInfo = uni.getSystemInfoSync();
			const query = uni.createSelectorQuery().in(that);
			query.select('.top-height').boundingClientRect();
			query.exec((res) => {
				that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
				console.log(that.topHeight, res[0].height);
				setTimeout(() => {
					that.loading = true;
				}, 0);
			});
		},
		// 去详情
		navToDetailFunc(item) {
			uni.navigateTo({
				url: '/pagesB/constructionLogDetail/constructionLogDetail?id=' + item.id
			});
		},
		queryList(page, num) {
			this.constructionLogList(page, num);
		},
		constructionLogList(page, num) {
			uni.showLoading({
				mask: true
			});
			let searchForm = this.searchForm;
			this.$Apipc
				.constructionLogList({
					page,
					num,
					project_name: searchForm.project_name,
					store_type_id: searchForm.store_type_id,
					risk_label: searchForm.risk_label,
					construction_stage_id: searchForm.construction_stage_id,
					is_overdue: searchForm.is_overdue,
					estimated_completion_date_start: searchForm.estimated_completion_date_start,
					estimated_completion_date_end: searchForm.estimated_completion_date_end
				})
				.then((res) => {
					uni.hideLoading();
					console.log(res, '施工日志');
					if (res.code == 200) {
						this.$refs.paging.complete(res.data.list);
					}
				});
		},
		// 门店类型列表(项目类型)
		getStoreTypeList() {
			this.$Apipc
				.getStoreTypeList({
					page: this.searchListObj.store_type_id.page,
					num: this.searchListObj.store_type_id.num
				})
				.then((res) => {
					console.log(res, '门店类型列表');
					if (res.code == 200) {
						if (this.searchListObj.store_type_id.page == 1) {
							this.searchListObj.store_type_id.list = [];
						}
						this.searchListObj.store_type_id.list = [...this.searchListObj.store_type_id.list, ...res.data.list];
						this.searchListObj.store_type_id.max = res.data.max;
					}
				});
		},
		// 弹窗筛选项
		searchSelectFunc(type, item) {
			console.log(type, item);
			switch (type) {
				case 'store_type_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'risk_label':
					this.searchForm[type] = this.searchForm[type] == item.label ? '' : item.label;
					break;
				case 'construction_stage_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'is_overdue':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				default:
			}
		},
		// 弹窗筛选项查看更多
		lookMoreFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page += 1;
					this.getStoreTypeList();
					break;
				case 'engineering_manager_id':
					this.searchListObj.engineering_manager_id.page += 1;
					this.getEngineeringManager();
					break;
				case 'construction_stage_id':
					this.searchListObj.construction_stage_id.page += 1;
					this.getConstructionStage();
					break;
				default:
			}
		},
		// 弹窗筛选项列表收起
		retractFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page = 1;
					this.getStoreTypeList();
					break;
				case 'engineering_manager_id':
					this.searchListObj.engineering_manager_id.page = 1;
					this.getEngineeringManager();
					break;
				case 'construction_stage_id':
					this.searchListObj.construction_stage_id.page = 1;
					this.getConstructionStage();
					break;
				default:
			}
		},
		// 施工阶段
		getConstructionStage() {
			this.$Apipc
				.getConstructionStage({
					page: this.searchListObj.construction_stage_id.page,
					num: this.searchListObj.construction_stage_id.num
				})
				.then((res) => {
					console.log(res, '施工阶段');
					if (res.code == 200) {
						if (this.searchListObj.construction_stage_id.page == 1) {
							this.searchListObj.construction_stage_id.list = [];
						}
						this.searchListObj.construction_stage_id.list = [...this.searchListObj.construction_stage_id.list, ...res.data.list];
						this.searchListObj.construction_stage_id.max = res.data.max;
					}
				});
		},
		// 创建时间日历弹窗
		chooseDateFunc() {
			this.dateShow = true;
		},
		// 日期筛选确认
		dateConfirmFunc(e) {
			this.searchForm.estimated_completion_date_start = e[0];
			this.searchForm.estimated_completion_date_end = e[e.length - 1];
			this.searchForm.estimated_completion_date_show = e[0] + '~' + e[e.length - 1];
			this.dateShow = false;
		},
		toAdd() {
			uni.navigateTo({
				url: '/pagesC/constructionLog/constructionLog'
			});
		}
	}
};
</script>

<style lang="scss"></style>